﻿using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Linq;
using System.Linq.Dynamic;
using System.Text;
using System.Threading.Tasks;
using Abp;
using Abp.Application.Services.Dto;
using Abp.Authorization;
using Abp.AutoMapper;
using Abp.Configuration;
using Abp.Domain.Repositories;
using Abp.Extensions;
using Abp.Linq.Extensions;
using LeadThing.AbpZeroTemplate.WorkFlows.Authorization;
using LeadThing.AbpZeroTemplate.WorkFlows.Dtos;
using LeadThing.AbpZeroTemplate.Dto;

namespace LeadThing.AbpZeroTemplate.WorkFlows
{
    /// <summary>
    /// 流程表-名称信息服务实现
    /// </summary>
    //[AbpAuthorize(WorkFlowAppPermissions.WorkFlow)]


    public class WorkFlowAppService : AbpZeroTemplateAppServiceBase, IWorkFlowAppService
    {
        private readonly IRepository<WorkFlow, long> _workFlowRepository;
        private readonly IWorkFlowListExcelExporter _workFlowListExcelExporter;


        private readonly WorkFlowManage _workFlowManage;
        /// <summary>
        /// 构造方法
        /// </summary>
        public WorkFlowAppService(IRepository<WorkFlow, long> workFlowRepository,
WorkFlowManage workFlowManage
      , IWorkFlowListExcelExporter workFlowListExcelExporter
  )
        {
            _workFlowRepository = workFlowRepository;
            _workFlowManage = workFlowManage;
            _workFlowListExcelExporter = workFlowListExcelExporter;
        }


        #region 实体的自定义扩展方法
        private IQueryable<WorkFlow> _workFlowRepositoryAsNoTrack => _workFlowRepository.GetAll().AsNoTracking();

        /// <summary>
        /// 获取所有流程
        /// </summary>
        /// <returns></returns>
        public async Task<ListResultDto<WorkFlowListDto>> GetAllWorkFlowListAsync()
        {
            var query = _workFlowRepositoryAsNoTrack;
            var items = await query.ToListAsync();

            var workFlowListDtos = items.MapTo<List<WorkFlowListDto>>();
            return new ListResultDto<WorkFlowListDto>(workFlowListDtos);
        }

        #endregion


        #region 流程表-名称信息管理

        /// <summary>
        /// 根据查询条件获取流程表-名称信息分页列表
        /// </summary>
        public async Task<PagedResultDto<WorkFlowListDto>> GetPagedWorkFlowsAsync(GetWorkFlowInput input)
        {

            var query = _workFlowRepositoryAsNoTrack;
            //TODO:根据传入的参数添加过滤条件

            var workFlowCount = await query.CountAsync();

            var workFlows = await query
            .OrderBy(input.Sorting)
            .PageBy(input)
            .ToListAsync();

            var workFlowListDtos = workFlows.MapTo<List<WorkFlowListDto>>();
            return new PagedResultDto<WorkFlowListDto>(
            workFlowCount,
            workFlowListDtos
            );
        }

        /// <summary>
        /// 通过Id获取流程表-名称信息信息进行编辑或修改 
        /// </summary>
        public async Task<GetWorkFlowForEditOutput> GetWorkFlowForEditAsync(NullableIdDto<long> input)
        {
            var output = new GetWorkFlowForEditOutput();

            WorkFlowEditDto workFlowEditDto;

            if (input.Id.HasValue)
            {
                var entity = await _workFlowRepository.GetAsync(input.Id.Value);
                workFlowEditDto = entity.MapTo<WorkFlowEditDto>();
            }
            else
            {
                workFlowEditDto = new WorkFlowEditDto();
            }

            output.WorkFlow = workFlowEditDto;
            return output;
        }


        /// <summary>
        /// 通过指定id获取流程表-名称信息ListDto信息
        /// </summary>
        public async Task<WorkFlowListDto> GetWorkFlowByIdAsync(EntityDto<long> input)
        {
            var entity = await _workFlowRepository.GetAsync(input.Id);

            return entity.MapTo<WorkFlowListDto>();
        }







        /// <summary>
        /// 新增或更改流程表-名称信息
        /// </summary>
        public async Task CreateOrUpdateWorkFlowAsync(CreateOrUpdateWorkFlowInput input)
        {
            if (input.WorkFlowEditDto.Id.HasValue)
            {
                await UpdateWorkFlowAsync(input.WorkFlowEditDto);
            }
            else
            {
                await CreateWorkFlowAsync(input.WorkFlowEditDto);
            }
        }

        /// <summary>
        /// 新增流程表-名称信息
        /// </summary>
        //[AbpAuthorize(WorkFlowAppPermissions.WorkFlow_CreateWorkFlow)]
        public virtual async Task<WorkFlowEditDto> CreateWorkFlowAsync(WorkFlowEditDto input)
        {
            //TODO:新增前的逻辑判断，是否允许新增

            var entity = input.MapTo<WorkFlow>();

            entity = await _workFlowRepository.InsertAsync(entity);
            return entity.MapTo<WorkFlowEditDto>();
        }

        /// <summary>
        /// 编辑流程表-名称信息
        /// </summary>
        //[AbpAuthorize(WorkFlowAppPermissions.WorkFlow_EditWorkFlow)]
        public virtual async Task UpdateWorkFlowAsync(WorkFlowEditDto input)
        {
            //TODO:更新前的逻辑判断，是否允许更新

            var entity = await _workFlowRepository.GetAsync(input.Id.Value);
            input.MapTo(entity);

            await _workFlowRepository.UpdateAsync(entity);
        }

        /// <summary>
        /// 删除流程表-名称信息
        /// </summary>
        //[AbpAuthorize(WorkFlowAppPermissions.WorkFlow_DeleteWorkFlow)]
        public async Task DeleteWorkFlowAsync(EntityDto<long> input)
        {
            //TODO:删除前的逻辑判断，是否允许删除
            await _workFlowRepository.DeleteAsync(input.Id);
        }

        /// <summary>
        /// 批量删除流程表-名称信息
        /// </summary>
        //[AbpAuthorize(WorkFlowAppPermissions.WorkFlow_DeleteWorkFlow)]
        public async Task BatchDeleteWorkFlowAsync(List<long> input)
        {
            //TODO:批量删除前的逻辑判断，是否允许删除
            await _workFlowRepository.DeleteAsync(s => input.Contains(s.Id));
        }

        #endregion

        #region 流程表-名称信息的Excel导出功能


        public async Task<FileDto> GetWorkFlowToExcel()
        {
            var entities = await _workFlowRepository.GetAll().ToListAsync();

            var dtos = entities.MapTo<List<WorkFlowListDto>>();

            var fileDto = _workFlowListExcelExporter.ExportWorkFlowToFile(dtos);



            return fileDto;
        }


        #endregion

    }
}
